{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df7da2f0-1125-4aa9-a31b-ea881f528e23",
   "metadata": {},
   "outputs": [],
   "source": [
    "我们首先导入PyTroch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "678cc5e2-29a0-4968-98ad-90aca1918ff5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4a8271f-b325-4a7c-bd4b-7d499d26f37d",
   "metadata": {},
   "outputs": [],
   "source": [
    "现在创建一个5×3的一个的未初始化Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66e288c5-1e23-47f5-92e7-a63ee3bd2e00",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'torch' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241m.\u001b[39mempty(\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m3\u001b[39m)\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(x)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'torch' is not defined"
     ]
    }
   ],
   "source": [
    "x = torch.empty(5,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7092822-b579-4ca3-ba9d-2d3c5d1ddd38",
   "metadata": {},
   "outputs": [],
   "source": [
    "创建一个5×3的随机初始化Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "575ff930-ad62-4f56-ae45-fd6e19d506b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.8210, 0.9071, 0.1393],\n",
      "        [0.7747, 0.7890, 0.9769],\n",
      "        [0.2427, 0.4534, 0.7236],\n",
      "        [0.7429, 0.4579, 0.9825],\n",
      "        [0.2038, 0.4467, 0.5648]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand(5,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be06bc37-014c-449d-a79b-50a80997c930",
   "metadata": {},
   "outputs": [],
   "source": [
    "创建一个5×3型全为0的Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "36e3ecb9-3181-4310-9ac6-9e8e4eac96b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.zeros(5,3,dtype = torch.long)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92455692-0cfe-4539-8d03-54093c187e48",
   "metadata": {},
   "outputs": [],
   "source": [
    "根据数据创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80cb89fb-3df5-435a-8173-461b644071f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([5.5000, 3.0000])\n"
     ]
    }
   ],
   "source": [
    "x = torch.tensor([5.5,3])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7460af4d-ef69-4547-89db-d5ee6177b037",
   "metadata": {},
   "outputs": [],
   "source": [
    "使用现有的tensor，修改相关属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7e50b021-ed84-4310-850b-4a2fb01c32c8",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'torch' has no attribute 'new_one'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnew_one\u001b[49m(\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m3\u001b[39m, dtype \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mfloat64)\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(x)\n",
      "File \u001b[1;32m~\\.conda\\envs\\deep_learning\\lib\\site-packages\\torch\\__init__.py:2563\u001b[0m, in \u001b[0;36m__getattr__\u001b[1;34m(name)\u001b[0m\n\u001b[0;32m   2560\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m _lazy_modules:\n\u001b[0;32m   2561\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m importlib\u001b[38;5;241m.\u001b[39mimport_module(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;18m__name__\u001b[39m)\n\u001b[1;32m-> 2563\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodule \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m has no attribute \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'torch' has no attribute 'new_one'"
     ]
    }
   ],
   "source": [
    "x = torch.new_one(5,3, dtype = torch.float64)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d94bbec4-c788-460d-bdc9-f371e3f851f7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9d6086e-31d5-4cec-afa9-457bbff38913",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pytorch]",
   "language": "python",
   "name": "deep_learning"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
